package cn.shujuhai.workflow.service.impl;

import cn.shujuhai.common.pluging.data.ResultCode;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.common.pluging.file.FilesUtils;
import cn.shujuhai.workflow.domain.Form;
import cn.shujuhai.workflow.domain.FormData;
import cn.shujuhai.workflow.mapper.FormMapper;
import cn.shujuhai.workflow.service.FormDataService;
import cn.shujuhai.workflow.service.FormService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ZhangDeZhong
 * @description 针对表【form_config】的数据库操作Service实现
 * @createDate 2025-03-11 09:40:20
 */
@Service
@RequiredArgsConstructor
public class FormServiceImpl extends ServiceImpl<FormMapper, Form>
        implements FormService {

    private final FormDataService formDataService;
    @Value("${template.static.formBackgroundUrl}")
    public String UPLOADS_PATH;
    @Value("${template.static.viewFormBackgroundUrl}")
    public String VIEW_PATH;

    @Override
    public ResultVo<?> getFormList(String formName, Integer formType, int pageNum, int pageSize) {
        String type = "";
        if (formType != null) {
            type = String.valueOf(formType);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Form> formList = this.list(Wrappers.<Form>lambdaQuery()
                .like(Form::getFormName, formName)
                .like(Form::getFormType, type)
                .orderByDesc(Form::getFormId));
        PageInfo<Form> pageInfo = new PageInfo<>(formList);
        return ResultVo.success(pageInfo);
    }

    @Override
    public ResultVo<?> addForm(Form form) {
        boolean save = this.save(form);
        if (save) {
            return ResultVo.success();
        }
        return ResultVo.failure("表单新增失败");
    }

    @Override
    public ResultVo<?> updateForm(Form form) {
        int size = this.isFormFilled(form.getFormId());
        if (size > 0) {
            return ResultVo.failure("该表单已被填写，不能修改！");
        }
        boolean updatedById = this.updateById(form);
        if (updatedById) {
            return ResultVo.success();
        }
        return ResultVo.failure("表单修改失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<?> deleteForm(Long formId) {

        this.formDataService.remove(Wrappers.<FormData>lambdaQuery().eq(FormData::getFormId, formId));

        boolean remove = this.removeById(formId);
        if (remove) {
            return ResultVo.success();
        }

        return ResultVo.failure("表单删除失败");
    }

    @Override
    public ResultVo<?> enableForm(Long formId) {
        Form form = this.getById(formId);
        if (form == null) {
            return ResultVo.failure("表单不存在！");
        }
        int state = form.getStatus() == 0 ? 1 : 0;
        boolean update = this.update(Wrappers.<Form>lambdaUpdate()
                .eq(Form::getFormId, form.getFormId())
                .set(Form::getStatus, state));
        if (update) {
            return ResultVo.success();
        }
        return ResultVo.failure("表单启用状态修改失败");
    }

    @Override
    public int isFormFilled(Long formId) {
        List<FormData> list = this.formDataService.list(Wrappers.<FormData>lambdaQuery().eq(FormData::getFormId, formId));
        return list.size();
    }

    @Override
    public ResultVo<?> getFormById(Long formId) {
        Form form = this.getById(formId);
        if (form == null) {
            return ResultVo.failure("");
        }
        return ResultVo.success(form);
    }

    @Override
    public String uploadImg(MultipartFile file) {
        return FilesUtils.uploadFile(file, UPLOADS_PATH, VIEW_PATH);

    }

    @Override
    public ResultVo<?> saveFormStyle(Long formId, String background) {
        Form form = this.getById(formId);
        form.setBackground(background);
        if (this.updateById(form)) {
            return ResultVo.success();
        }
        return ResultVo.failure(ResultCode.FAILED);
    }

    @Override
    public ResultVo<?> getFieldDataByFormId(Long formId) {
        Form one = this.getOne(new QueryWrapper<Form>().lambda().eq(Form::getFormId, formId));
        return ResultVo.success(one);
    }
}




